﻿/*
 * Copyright (c) Seagull Consulting, Inc. All rights reserved. See License.txt in the project root for license information.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using SeagullConsulting.MvcAjaxControls.Web.Mvc.Helpers.Properties;

namespace SeagullConsulting.MvcAjaxControls.Web.Mvc.Helpers
{
    //Put it in the @Html namespace
    public static class AjaxDropdownHelper
    {
        //New extension method for @Html
        public static AjaxDropdownBuilder AjaxDropdown(this HtmlHelper helper, string name)
        {
            return new AjaxDropdownBuilder(helper, name);
        }
    }

    public interface IAjaxDropdown
    {
        AjaxDropdownBuilder Controller<T>() where T : class;
        AjaxDropdownBuilder Parent(string parent);
        AjaxDropdownBuilder OriginalValue(string originalValue);
        AjaxDropdownBuilder Filter(string filter);
        AjaxDropdownBuilder Postfix(string postfix);
        MvcHtmlString ToHtml();
    }

    public class AjaxDropdownBuilder : IAjaxDropdown
    {
        private HtmlHelper helper;
        private string name = String.Empty;
        private string controller = String.Empty;
        private string parent = String.Empty;
        private string originalValue = String.Empty;
        private string filter = String.Empty;
        private string postfix = String.Empty;

        public AjaxDropdownBuilder (HtmlHelper helper, string name)
        {
            this.helper = helper;
            this.name = name;
            if ((string) helper.ViewData["controller"] != String.Empty)
                controller = helper.ViewData["controller"].ToString().Replace("Controller", String.Empty);
        }

        public AjaxDropdownBuilder Controller<T>()
            where T : class
        {
            var type = typeof(T);
            this.controller = type.Name.Replace("Controller", String.Empty);
            return this;
        }

        public AjaxDropdownBuilder OriginalValue(string originalValue)
        {
            this.originalValue = originalValue;
            return this;
        }

        public AjaxDropdownBuilder Filter(string filter)
        {
            this.filter = filter;
            return this;
        }

        public AjaxDropdownBuilder Parent(string parent)
        {
            this.parent = parent;
            return this;
        }

        public AjaxDropdownBuilder Postfix(string postfix)
        {
            this.postfix = postfix;
            return this;
        }

        public MvcHtmlString ToHtml ()
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder scriptBuilder = new StringBuilder();
            String scriptText = Resources.AjaxDropdownScript;
            scriptText = scriptText.Replace("$name", name);
            if (postfix != String.Empty)
                scriptText = scriptText.Replace("$child", "'#" + name + postfix + "'");
            else
                scriptText = scriptText.Replace("$child", "'#" + name + "'");
            scriptText = scriptText.Replace("$controller", controller);
            if (filter != String.Empty)
                scriptText = scriptText.Replace("$filter", filter);
            else
                scriptText = scriptText.Replace("$filter", "null");
            if (parent != String.Empty)
                scriptText = scriptText.Replace("$parent", "'#" + parent + "'");
            else
                scriptText = scriptText.Replace("$parent", "null");

            //Build Select List
            TagBuilder select = new TagBuilder("select");
            if (postfix != String.Empty)
            {
                select.Attributes["id"] = name + postfix;
                select.Attributes["name"] = name + postfix;
            }
            else
            {
                select.Attributes["id"] = name;
                select.Attributes["name"] = name;
            }
            if (originalValue != String.Empty)
                select.Attributes["originalValue"] = originalValue;
            select.AddCssClass("AjaxDropdown");
            sb.Append(select.ToString());

            //Build Ajax Library Call
            TagBuilder script = new TagBuilder("script");
            script.Attributes["type"] = "text/javascript";
            scriptBuilder.Append(Environment.NewLine + scriptText + Environment.NewLine);
            script.InnerHtml = scriptBuilder.ToString();
            sb.Append(Environment.NewLine +  script.ToString());
            return new MvcHtmlString(sb.ToString());
        }
    }
}